#!/bin/sh

# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.

# git gotest pre-commit hook
#
# Runs go tests and also ensures that packages with no tests compile, ie
# the tests dir.

# Executes tests on the non-external packages (ie packages from github)
__DIR__="$(cd "$(dirname "${0}")"; echo $(pwd))"
source "$__DIR__/pkgs"

# run tests. go test outputs compilation errors to STDERR, which is why it's redirected.
tests=$(go test $PKGS -timeout=10s 2>&1)
[ $? == 0 ] && exit 0

# There are failing tests. Packages with compilation errors start with #, failures with FAIL. Print them and fail.
failing=$(echo "$tests" | grep '^\#\|^FAIL' | awk '{print $2}' | grep -ve '^$' | sort -u)

echo >&2 "There are failing tests or compilation errors. Please fix these packages:"
for pkg in $failing
do
  echo >&2 "  go test -v -short $pkg"
done

exit 1
